package com.baomidou.springmvc.service.system.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.springmvc.mapper.system.UserMapper;
import com.baomidou.springmvc.model.system.User;
import com.baomidou.springmvc.model.system.UserView;
import com.baomidou.springmvc.service.system.IUserService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * User 表数据服务层接口实现类
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public void crud() {
        crud1();
        crud2();
    }

    // auto ID
    void crud1() {
        // 初始化 影响行数
        int result = 0;
        // 初始化 User 对象
        User user = new User();

        // 插入 User (插入成功会自动回写主键到实体类)
        user.setName("Tom");
        result = baseMapper.insert(user);
        System.out.println("insert rows: " + result);
        System.out.println(user);

        // 更新 User
        user.setAge(18);
        result = baseMapper.updateById(user);
        System.out.println("update rows: " + result);
        System.out.println(user);

        // 查询 User
        User exampleUser = baseMapper.selectById(user.getId());
        System.out.println(exampleUser);

        // 查询姓名为‘张三’的所有用户记录
        List<User> userList = baseMapper.selectList(
                new EntityWrapper<User>().eq("name", "Tom")
        );
        System.out.println(userList);

        // 删除 User
        result = baseMapper.deleteById(user.getId());
        System.out.println("delete rows: " + result);
    }

    // my ID
    void crud2() {
        // 初始化 影响行数
        int result = 0;
        // 初始化 User 对象
        User user = new User();

        // 插入 User (插入成功会自动回写主键到实体类)
        long id = System.currentTimeMillis();
        System.out.println("id: " + id);
        user.setId(id);
        user.setName("Tom");
        result = baseMapper.insert(user);
        System.out.println("insert rows: " + result);
        System.out.println(user);

        // 更新 User
        user.setAge(18);
        result = baseMapper.updateById(user);
        System.out.println("update rows: " + result);
        System.out.println(user);

        // 查询 User
        User exampleUser = baseMapper.selectById(user.getId());
        System.out.println(exampleUser);

        // 查询姓名为‘张三’的所有用户记录
        List<User> userList = baseMapper.selectList(
                new EntityWrapper<User>().eq("name", "Tom")
        );
        System.out.println(userList);

        // 删除 User
        result = baseMapper.deleteById(user.getId());
        System.out.println("delete rows: " + result);
    }

    @Override
    public void crudRollback() {
        // 初始化 影响行数
        int result = 0;
        // 初始化 User 对象
        User user = new User();

        // 插入 User (插入成功会自动回写主键到实体类)
        user.setName("Tom");
        result = baseMapper.insert(user);
        System.out.println("insert rows: " + result);
        System.out.println(user);

        // 更新 User
        user.setAge(18);
        result = baseMapper.updateById(user);
        System.out.println("update rows: " + result);
        System.out.println(user);

        // 查询 User
        User exampleUser = baseMapper.selectById(user.getId());
        System.out.println(exampleUser);

        // 查询姓名为‘张三’的所有用户记录
        List<User> userList = baseMapper.selectList(
                new EntityWrapper<User>().eq("name", "Tom")
        );
        System.out.println(userList);

        throw new IllegalArgumentException("异常回滚测试");
    }

    @Override
    public Page<User> buildinSelectUserPage(Page<User> page, String state) {
        page.setSearchCount(true); // 查询总记录数
        List<User> userList = baseMapper.selectPage(
                page,
                new EntityWrapper<User>()
                        .eq("type", state)
                        .between("id", 1, 50)
        );
        return page.setRecords(userList);
    }

    @Override
    public Page<User> selectUserPage(Page<User> page, String state) {
        //page.setOptimizeCountSql(false); // 不进行 count sql 优化，解决 MP 无法自动优化 SQL 问题
        page.setSearchCount(true); // 查询总记录数
        return page.setRecords(baseMapper.selectUserList(page, state));
    }

    @Override
    public Page<UserView> selectUserView(Page<UserView> page, String state) {
        page.setSearchCount(true); // 查询总记录数
        return page.setRecords(baseMapper.selectUserView(page, state));
    }
}